#pragma once

#include <vector>
#include <string>
using namespace std;

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
typedef boost::filesystem::path path;
typedef boost::filesystem::directory_iterator directory_iterator;

#include "OutputConfig.h"

/**
 * This class manages a list of ABPA files that should
 * be parsed. By default it will return a list of all files
 * in the default output folder (according to current registry
 * settings). By using special command-line options, the user may
 * change the folder to look for files in, or/and list which files
 * exactly should be processed
 */
class OutputFiles {
	string _folder;
	vector<string> _requested_files;
	vector<string> _final_list_of_files;

public:
	OutputFiles(void) 
		: _folder(config::OutputConfig().getLocation().getANSIString())
	{}

	void setFolder(string f) { _folder = f; }

	void addFileName(string s) { _requested_files.push_back(s); }
	void addFileList(vector<string> ls) { _requested_files.insert(_requested_files.end(), ls.begin(), ls.end()); }

	vector<string> & getFilesToProcess(void) {
		doBuildFileList();
		return _final_list_of_files;
	}

private:
	// this function actually does the hard job of
	// building a list of files to be processed
	void doBuildFileList() {
		if (_requested_files.empty()) {
			addAllFiles();
			return;
		}

		if (_folder[_folder.length()-1] != '\\')
			_folder += '\\';

		for (vector<string>::iterator it = _requested_files.begin(); it != _requested_files.end(); ++it)
			if (it->find(':') == string::npos)
				// relative path was given
				_final_list_of_files.push_back(_folder + *it);
			else
				// full path
				_final_list_of_files.push_back(*it);
	}


	/**
	 * Just adds all available files to the list
	 */
	void addAllFiles() {
		directory_iterator end_itr; // default construction yields past-the-end
		regex abpaFile(".*\\.abpa$");
		for (directory_iterator itr(_folder); itr != end_itr; ++itr) {
			const path::string_type &next = itr->path().string();
			if (regex_match(next.c_str(), abpaFile))
				_final_list_of_files.push_back(next);		
		}
	}

};
